import { createRouter, createWebHashHistory } from 'vue-router'
import Home from '../views/Home.vue'
// 导入带有底部导航的一级页面路由
import firstLeval from '../views/firstLeval/route'
// console.log(firstLeval);
// 不带有底部导航的首页内部页面
import childPage from '../views/childPage/route'
const routes = [
  {
    path: '/',
    component: Home,
    redirect: '/index',
    children: [
      ...firstLeval
    ]
  },
  ...childPage,
  {
    path: '/model',
    component: () => import ('../views/vModel')
  },
  {
    path: '/test',
    component: () => import ('../views/test')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// router.beforeEach((to, from, next) => {
//   console.log(to);
//   // return false
//   next()
// })

router.beforeEach((to, from) => {
  // console.log(to);
  if(to.meta.needLogin) {
  // if(to.matched.some((route) => route.meta.needLogin)) {
    let token = localStorage.getItem('token')
    if(token != "undefined" && token) {
      return true
    } else {
      return {
        path: '/login'
      }
    }
  } else {
    return true
  }
})


router.afterEach((to) => {
  document.querySelector('title').innerText = to.meta.title
})

export default router
